﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Member
{
    public partial class AddMemberFrm : Form
    {
        private MySqlServer mysqlserver= new MySqlServer();

        public AddMemberFrm()
        {
            InitializeComponent();
            Begin_Add_Member();
            cb_card_type.SelectedIndexChanged += cb_card_type_SelectedIndexChanged;
            txt_card_point.KeyPress += txt_card_point_KeyPress;
            txt_card_id.KeyPress += txt_card_id_KeyPress;
        }

        void txt_card_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        void txt_card_point_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        void cb_card_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Util.IsValidDateFormat(txt_card_join_date.Text))
            {
                DateTime dt = DateTime.Parse(txt_card_join_date.Text);
                switch (cb_card_type.SelectedIndex)
                {
                    case 1:
                        dt = dt.AddMonths(6);
                        break;
                    case 2:
                        dt = dt.AddYears(1);
                        break;
                    case 3:
                        dt = dt.AddYears(80);
                        break;
                }
                txt_card_expire_date.Text = dt.Year.ToString() + "/" + dt.Month.ToString() + "/" + dt.Day.ToString();
            }
        }
        private void resetInput()
        {
            txt_card_id.Text = "";
            txt_customer_name.Text = "";
            txt_card_point.Text = "";
            txt_customer_phone.Text = "";
            txt_customer_birthday.Text = "";
            txt_customer_address.Text = "";
            cb_card_type.SelectedIndex = -1;
            txt_card_join_date.Text = "";
            txt_card_expire_date.Text = "";
        }
        private void Begin_Add_Member()
        {
            cb_card_type.SelectedIndex = 0;
            txt_card_point.Text = "0";
            txt_card_join_date.Text = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
            txt_card_expire_date.Text = (DateTime.Now.Year + 1).ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
        }
        private bool Add_Member()
        {
            string card_id = txt_card_id.Text;
            string full_name = txt_customer_name.Text;
            string point = txt_card_point.Text;
            string phone = txt_customer_phone.Text;
            string birthday = txt_customer_birthday.Text;
            string address = txt_customer_address.Text;
            int card_type = cb_card_type.SelectedIndex;
            string join_day = txt_card_join_date.Text;
            string expire_day = txt_card_expire_date.Text;
            //check input
            if (card_id == "" || full_name == "" || point == "" || card_type < 0 || join_day == "" || expire_day == "")
            {
                MessageBox.Show("Missed customer's informations");
                return false;
            }
            if (!Util.IsValidDateFormat(join_day) || !Util.IsValidDateFormat(expire_day) || (birthday != "" && !Util.IsValidDateFormat(birthday)))
            {
                MessageBox.Show("Please input correct format 'year/month/day'");
                return false;
            }
            else
            {
                if (DateTime.Parse(expire_day) < DateTime.Parse(join_day))
                {
                    MessageBox.Show("Day expire must larger than Day join");
                    return false;
                }
            }
            //
            string query = @"Insert Into CUSTOMER (card_id, full_name, point, phone, birthday, address, is_vip, join_date, expire_date) 
                            Values ('" + card_id + "','" + full_name + "','" + point + "','" + phone + "','" + birthday + "','" + address + "','" + card_type + "','" + join_day + "','" + expire_day + "')";
            if (mysqlserver.ExecuteNonQuery(query) > 0)
            {
                MessageBox.Show("Add successful");
                return true;
            }
            return false;

        }

        private void bt_Ok_Click(object sender, EventArgs e)
        {
            if(Add_Member())
                this.Close();
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
